package Caoshunqi;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Scanner;

class DoubleColorBall {
    public static void main(String[] args) {
        Random ran = new Random();

        // 生成中奖号码
        int xtBlue = ran.nextInt(16) + 1;
        LinkedHashSet<Integer> xtRed = new LinkedHashSet<>();
        while (xtRed.size() < 6) {
            int red = ran.nextInt(33) + 1;
            xtRed.add(red);
        }

        // 用户输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入 1 - 16 的蓝球号码：");
        int khBlue = getValidBlueInput(sc);

        LinkedHashSet<Integer> khRed = new LinkedHashSet<>();
        while (khRed.size() < 6) {
            System.out.println("请输入第 " + (khRed.size() + 1) + " 个红球号码（1-33，不能重复）：");
            int number = getValidRedInput(sc);
            khRed.add(number);
        }
        System.out.println("\n=== 您的投注 ===");
        System.out.println("红球：" + khRed + "  蓝球：" + khBlue);

        // 计算中奖详情
        int matchedRedCount = 0;
        LinkedHashSet<Integer> matchedRedNumbers = new LinkedHashSet<>();
        for (Integer userRed : khRed) {
            if (xtRed.contains(userRed)) {
                matchedRedCount++;
                matchedRedNumbers.add(userRed);
            }
        }
        boolean blueMatched = (khBlue == xtBlue);

        // 输出开奖结果
        System.out.println("\n=== 开奖号码 ===");
        System.out.println("红球：" + xtRed + "  蓝球：" + xtBlue);

        // 输出中奖详情
        System.out.println("\n=== 中奖详情 ===");
        System.out.println("红球中奖：" + matchedRedCount + "个");
        System.out.println("中奖红球号码：" + (matchedRedNumbers.isEmpty() ? "无" : matchedRedNumbers));
        System.out.println("蓝球中奖：" + (blueMatched ? "中奖" : "未中奖"));

        // 判断奖项
        System.out.println("\n=== 奖项判定 ===");
        if (blueMatched) {
            switch (matchedRedCount) {
                case 6:
                    System.out.println("一等奖！6红+1蓝");
                    break;
                case 5:
                    System.out.println("三等奖！5红+1蓝");
                    break;
                case 4:
                    System.out.println("四等奖！4红+1蓝");
                    break;
                case 3:
                    System.out.println("五等奖！3红+1蓝");
                    break;
                case 2:
                    System.out.println("六等奖！2红+1蓝");
                    break;
                case 1:
                    System.out.println("六等奖！1红+1蓝");
                    break;
                default:
                    System.out.println("六等奖！蓝球中奖");
            }
        } else {
            switch (matchedRedCount) {
                case 6:
                    System.out.println("二等奖！6红+0蓝");
                    break;
                case 5:
                    System.out.println("四等奖！5红+0蓝");
                    break;
                case 4:
                    System.out.println("五等奖！4红+0蓝");
                    break;
                default:
                    System.out.println("很遗憾，您未中奖");
            }
        }
        sc.close();
    }

    private static int getValidBlueInput(Scanner sc) {
        while (true) {
            int input = sc.nextInt();
            if (input >= 1 && input <= 16) return input;
            System.out.println("输入无效，请输入1-16之间的数字：");
        }
    }

    private static int getValidRedInput(Scanner sc) {
        while (true) {
            int input = sc.nextInt();
            if (input >= 1 && input <= 33) return input;
            System.out.println("输入无效，请输入1-33之间的数字：");
        }
    }
}
